<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bg{
            background-color: red;
        }
    </style>
</head>
<body>
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button>4</button>
    <div>
        <img src="./2.jpg" alt="">
    </div>

    <script>
        let btn=document.getElementsByTagName("button");
        let img=document.getElementsByTagName("img")[0];

        let arr=["2.jpg","123.jpg","asd.jpg","hello.jpg"];

        for(let i=0;i<btn.length;i++){
            btn[i].onclick=function(){
                // 方法一：设置属性值，通用方法修改元素属性
                // img.setAttribute("src",`./${i+1}.jpg`);

                // 方法二：
                img.src=`./${arr[i]}`;
                for(let j=0;j<btn.length;j++){
                    btn[j].classList.remove("bg");
                }
                btn[i].classList.add("bg");
            }
        }

    </script>
</body>
</html>